package org.lztvn.elearning.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.lztvn.elearning.entities.Command;
import org.lztvn.elearning.entities.Product;



public class CommandDaoImpl implements ICommandDao{
	@PersistenceContext
	private EntityManager em;
	@Override
	public Long addCommand(Command c) {
		em.persist(c);
		return c.getIdCmd();
	}

	@Override
	public void deleteCommand(Long idCom) {
		Command c = em.find(Command.class, idCom);
		em.remove(c);
	}

	@Override
	public void modifierCommand(Command c) {
		em.merge(c);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Command> listCommand() {
		Query req = em.createQuery("select c from Command c");
		return req.getResultList();
	}

	@Override
	public Command getCommand(Long idCom) {
		return em.find(Command.class, idCom);
	}
	
	@Override
	public List<Command> listCommandByUserId(Long idUser, int position, int nb_com) {
		Query req = em.createQuery("select c from Command c where idUser = :x order by c.dateCmd desc");
		req.setParameter("x",idUser);
//		req.setFirstResult(position);
//		req.setMaxResults(nb_com);
		return req.getResultList();
	}

	@Override
	public List<Command> listCommand(int position, int nb_com) {
		Query req = em.createQuery("select c from Command c order by c.dateCmd desc");
		return req.getResultList();
	}

}
